package com.dfiov.cloud.msmtools.entity.vo.msbluetooth;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class KeyPermission {
	@ApiModelProperty("车门锁")
	private int doorLock = 0;
	@ApiModelProperty("尾门锁")
	private int tailDoorLock = 0;
	@ApiModelProperty("车窗")
	private int carWindow = 0;
	@ApiModelProperty("天窗")
	private int skylight = 0;
	@ApiModelProperty("寻车（闪灯鸣笛）")
	private int findCar = 0;
	@ApiModelProperty("泊车")
	private int parkCar = 0;

	public String toJson() {
		return JSON.toJSONString(this);
	}

	public int toBin() {
		return doorLock + (tailDoorLock << 1) + (tailDoorLock << 2) + (carWindow << 3) + (skylight << 4)
				+ (findCar << 5) + (parkCar << 6);
	}

	public static KeyPermission formBin(int permission) {
		KeyPermission result = new KeyPermission();
		if ((permission & 1) > 0) {
			result.setDoorLock(1);
		}

		if (((permission >> 2) & 1) > 0) {
			result.setTailDoorLock(1);
		}

		if (((permission >> 3) & 1) > 0) {
			result.setCarWindow(1);
		}
		if (((permission >> 4) & 1) > 0) {
			result.setSkylight(1);
		}
		if (((permission >> 5) & 1) > 0) {
			result.setFindCar(1);
		}
		if (((permission >> 6) & 1) > 0) {
			result.setParkCar(1);
		}

		return result;
	}

	public static KeyPermission buildAllPermission() {
		KeyPermission result = new KeyPermission();
		result.setDoorLock(1);
		result.setTailDoorLock(1);
		result.setCarWindow(1);
		result.setSkylight(1);
		result.setFindCar(1);
		result.setParkCar(1);
		return result;
	}
	
	public static KeyPermission buildNoPermission() {
		KeyPermission result = new KeyPermission();
		result.setDoorLock(0);
		result.setTailDoorLock(0);
		result.setCarWindow(0);
		result.setSkylight(0);
		result.setFindCar(0);
		result.setParkCar(0);
		return result;
	}

}
